#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QDebug>
#include <QTimer>
#include <QMutex>
#include <QDateTime>
#include <QMainWindow>
#include <QHeaderView>
#include <QTableWidget>
#include <QMutexLocker>
#include <QtCharts/QChart>
#include <QSystemTrayIcon>
#include "include/TrGlobal.h"
#include "include/TrManager.h"
#include "include/TrService.h"
#include "include/TrDateTime.h"
#include "include/TrFollowTrader.h"
#include "include/TrDataStore.h"
#include "include/TrMdKlineChart.h"
#include "include/TrMdRightBar.h"
#include "include/TrSocket.h"
#include "DualThrust.h"

#ifdef Q_OS_WIN
#pragma comment(lib, "user32.lib")
#include <qt_windows.h>
#endif

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

Q_DECLARE_METATYPE(QVector<int>)
Q_DECLARE_METATYPE(CThostFtdcDepthMarketDataField)
Q_DECLARE_METATYPE(CThostFtdcOrderField)
Q_DECLARE_METATYPE(CThostFtdcTradeField)
Q_DECLARE_METATYPE(CThostFtdcRspUserLoginField)
Q_DECLARE_METATYPE(CThostFtdcTradingAccountField)
Q_DECLARE_METATYPE(CThostFtdcInvestorPositionField)
Q_DECLARE_METATYPE(QVector<CThostFtdcInvestorPositionField>*)
//==================================================
extern std::shared_ptr<CTrManager> g_pManager;
extern std::shared_ptr<CTrSocket> g_pSocket;
//==================================================
QT_CHARTS_USE_NAMESPACE


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow, public CTrReceiver
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow() override;
    virtual void OnReceiveData(Tr_ResponseID rId, void* wParam, void* lParam) override;
private:
    Ui::MainWindow *ui;

public:
    void Init_Ctrl();
    void Init_ToolBar();
    void Init_Table();
    void Init_setTableStyleSheet(QTableWidget *t, QHeaderView::ResizeMode mode);
    void HeartBeat();
    QString OffsetFlagType(TThostFtdcOffsetFlagType offset);
    void OrderSubmitStatus(QTableWidget* table, TThostFtdcOrderStatusType state, int iRow);
    void    tray_create();
    QMap<QString, CThostFtdcInstrumentField>    m_Instruments;
    QColor      PriceRed;
    QColor      PriceRedp;
    QColor      PriceGre;
    QColor      PriceGrep;
    QColor      PriceOrg;
    QColor      PriceOrgp;
    QColor      PriceBlu;
    QColor      PriceBlup;
    QColor      PriceBla;
    QColor      PriceBlap;
protected:
    // Vars
    bool    m_bRunning = false;
    QAction* m_pAction_Start;
    QAction* m_pAction_Stop;
    QAction* m_pAction_DualStrategy;
    QVector<CThostFtdcInvestorPositionField>    m_vPositions;
    QVector<CThostFtdcOrderField>               m_vOrders;
    QMap<QString, QVariant>                     m_mapDailyHistory;

    QString     m_BrokerID;
    QString     m_UserID;
    QString     m_Password;
    QString     m_AuthCode;
    QString     m_AppID;
    QString     m_MarketFront;
    QString     m_TraderFront;
    QStringList     m_OnTradedSendTo;

    CTrDataStore*   m_DataStore;
    CTrMdKlineChart*    m_pKlineChart;
    CTrMdRightBar*      m_pRightBar;
    DualThrust*         m_pThrust;
    // Functions
    void    timerEvent(QTimerEvent *) override;
    void    closeEvent(QCloseEvent *event) override;
    void    update_ctrl();
    void    logbox(QString log);
    void    coverOrder(CThostFtdcOrderField order);
    void    OnRtnOrder(CThostFtdcOrderField order);
    void    LoadXmlConfig();
    virtual bool nativeEvent(const QByteArray &eventType, void *message, long *result) override;
    bool    winEvent(MSG *message, long *result);
    void    SendMessage_OnTrade(CThostFtdcTradeField *pTrade);
    void    OnDailyHistory(QString s);
protected slots:
    void    Slot_Action_Start();
    void    Slot_Action_Stop();
    void    Slot_Action_CreateOrder();
    void    Slot_OnDepthMarketData(QVariant dataVar);
    void    Slot_OnTradingAccount(QVariant dataVar);
    void    Slot_OnInvestorPositions(QVariant dataVar);
    void    Slot_OnRtnOrder(QVariant dataVar);
    void    Slot_OnTrade(QVariant dataVar);
    void    Slot_OnActivated(QSystemTrayIcon::ActivationReason reason);
    void    Slot_OnTableOneDBCheck(QTableWidgetItem* item);
    void    Slot_Action_Setting();
    void    Slot_Action_Strategy();
private:
    CTrFollowTrader*    m_Follower;
    QSystemTrayIcon*    m_sysTray;
    int                 m_iTimer;
    int                 m_iMarketDataCount = 0;
    int                 m_iReceiverCount = 0;
    CTrDateTime         m_tDTime;
    bool                m_bInstrumentUpdated = false;
    QMutex              m_tOneMutex;
signals:
    void    OnDepthMarketData(QVariant dataVar);
    void    OnTradingAccount(QVariant dataVar);
    void    OnInvestorPosition(QVariant dataVar, bool bIsLast);
    void    OnInvestorPositions(QVariant dataVar);
    void    OnRtnOrder(QVariant dataVar);
    void    OnTrade(QVariant dataVar);
};

#endif // MAINWINDOW_H
